package 刷题.leedcode21;
//递归法
public class Solution2 {
    public ListNode mergeTwoLists(ListNode list1,ListNode list2){
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                list1.next = mergeTwoLists(list1.next,list2);
                return list1;
            }else{
                list2.next = mergeTwoLists(list1,list2.next);
                return list2;
            }
        }
        //其中一个链表为空时的操作，类似Solution1
        return list1 == null ? list2 : list1;
    }
}
